package com.hotec.PROJECT_NAME.modules.sys.mapper;

import com.hotec.PROJECT_NAME.component.basic.BasicMapper;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysResource;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;


/**
 * @author llb
 */
public interface SysResourceMapper extends BasicMapper<SysResource> {

    /**
     * 根据用户的账号，获取用户的导航栏
     *
     * @param currentAccountId
     * @return
     */
    @SelectProvider(type = ResProvider.class, method = "queryNavigateByAccountId")
    List<SysResource> queryNavigateByAccountId(String currentAccountId);

    /**
     * 根据用户角色获取资源列表
     *
     * @param roleId
     * @return
     */
    @SelectProvider(type = ResProvider.class, method = "findResByRoleId")
    List<SysResource> findResByRoleId(String roleId);


    class ResProvider {
        public String findResByRoleId(String roleId) {
            StringBuilder sb = new StringBuilder("select res.* from sys_resource res ,sys_role_resource srr where res.id = srr.resource_id and srr.role_id=#{roleId} and res.state='1' AND  res.del_flag='0' and res.show_flag='true'");
            return sb.toString();
        }

        public String queryNavigateByAccountId(String accountId) {
            StringBuilder sb = new StringBuilder("select DISTINCT r.* from sys_role_resource rr , sys_role_account rl,sys_resource r " +
                    "where rr.role_id= rl.role_id  AND rl.account_id = #{accountId} and rr.resource_id= r.id and r.del_flag='0' and r.state='1' order by r.sort_ asc");
            return sb.toString();
        }
    }
}

